import { cloneDeep } from 'lodash-es';

interface Store {
	$id: string;
	$state: Record<string, unknown>;
	$patch: (state: Record<string, unknown>) => void;
	$reset?: () => void; // 可选的 $reset 方法
}

interface Context {
	store: Store;
}

/**
 * setup语法的重置状态插件
 * @param context
 * @description 请将用 setup 语法的状态 id 写入到 setupSyntaxIds
 */
export function resetSetupStore(context: Context): void {
	const setupSyntaxIds: string[] = ['setup-store'];

	if (setupSyntaxIds.includes(context.store.$id)) {
		const { $state } = context.store;

		const defaultStore: Record<string, unknown> = cloneDeep($state);

		context.store.$reset = () => {
			context.store.$patch(defaultStore);
		};
	}
}
